package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.CommentGoodsModel;
import com.minyifang.wcspf.work.ui.user.model.UploadPicBean;
import com.minyifang.wcspf.work.ui.user.view.CommitCommentView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CommitCommentPresenter extends BasePresenter<CommitCommentView> {
    /**
     * 上传图片文件
     *
     * @param imgFile
     */
    public void updateHeadImg(File imgFile) {
        // 创建RequestBody，传入参数："multipart/form-data"，String
        RequestBody requestToken = RequestBody.create(MediaType.parse("multipart/form-data"), UserManager.getInstance().getToken());
        RequestBody requestClient = RequestBody.create(MediaType.parse("multipart/form-data"), "android");
        // 创建RequestBody，传入参数："multipart/form-data"，File
        RequestBody requestImgFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);
        // 创建MultipartBody.Part，用于封装文件数据
        MultipartBody.Part requestImgPart = MultipartBody.Part.createFormData("files", imgFile.getName(), requestImgFile);
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().uploadImage(requestToken, requestClient, requestImgPart), new Consumer<HttpRespond<List<UploadPicBean>>>() {
            @Override
            public void accept(HttpRespond<List<UploadPicBean>> listHttpRespond) throws Exception {
                getView().hideLoadingView();
                if (listHttpRespond.result == 1) {
                    getView().onUploadPicSucceed(listHttpRespond.data);
                }
            }
        });
    }

    /**
     * 获取评论商品信息
     *
     * @param orderId
     */
    public void getCommitOrderInfo(String orderId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCommitOrderInfo(requestBody), new Consumer<HttpRespond<String>>() {
            @Override
            public void accept(HttpRespond<String> stringHttpRespond) throws Exception {
                if (stringHttpRespond.result == 1) {
                    String data = SafeUtils.decrypt(stringHttpRespond.data);
                    LogUtil.e(data);
                    List<CommentGoodsModel> list = new Gson().fromJson(data, new TypeToken<List<CommentGoodsModel>>() {
                    }.getType());
                    getView().onGetCommitOrderInfoSucceed(list);
                } else {
                    getView().onRequestFailed(stringHttpRespond.message);
                }
            }
        });
    }

    /**
     * 添加评论
     *
     * @param orderId
     * @param goodsModelList
     */
    public void addComment(String orderId, List<CommentGoodsModel> goodsModelList,int isAnonymous) {
//        isAnonymous		是否匿名(1匿名;0不匿名)
        JSONArray array = new JSONArray();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("orderId", orderId);
        dataMap.put("isAnonymous", isAnonymous);
        try {
            for (CommentGoodsModel commentGoodsModel : goodsModelList) {
                JSONObject object = new JSONObject();
                array.put(object);
                object.put("comId", commentGoodsModel.getID());
                object.put("productScore", commentGoodsModel.getGoodScore());
                object.put("serviceScore", commentGoodsModel.getServiceScore());
                object.put("productDes", commentGoodsModel.getGoodsComment());
                object.put("serviceDes", commentGoodsModel.getServiceComment());
                object.put("pic", commentGoodsModel.getPicListStr());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        dataMap.put("evaluation", array);
        JSONObject jsonObject = Constants.getAESJsonData(dataMap, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().addComment(requestBody), new Consumer<HttpRespond>() {
            @Override
            public void accept(HttpRespond httpRespond) throws Exception {
                getView().hideLoadingView();
                if (httpRespond.result == 1) {
                    getView().onAddCommentSucceed(httpRespond.message);
                } else {
                    getView().onRequestFailed(httpRespond.message);
                }
            }
        });
    }
}
